package com.banciyuan.bcywebview.biz.main.mineinfo.reward;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.api.GiftApi;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.biz.post.Listener.OnTextChangeListener;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.StringRequestParam;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;

import com.banciyuan.bcywebview.utils.http.BcyPair;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by squall on 2016/5/27.
 */
public class EditRewardActivity extends BaseActivity implements View.OnClickListener {


    private TextView close;
    private TextView set;
    private EditText content_et;
    private RelativeLayout container;
    private TextView show_length;
    private boolean click_flag;
    private RequestQueue queue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.openreward_edit);
        queue = VolleyQueue.getRquest(this);
        initUi();
    }

    @Override
    protected void initUi() {
        close = (TextView) findViewById(R.id.iv_cancle);
        set = (TextView) findViewById(R.id.tv_finish);
        container = (RelativeLayout) findViewById(R.id.container);
        show_length = (TextView) findViewById(R.id.show_length);
        content_et = (EditText) findViewById(R.id.et_comment);
        content_et.addTextChangedListener(new OnTextChangeListener() {
            @Override
            public void afterTextChanged(Editable s) {
                show_length.setText(s.length() + "/30");
            }
        });
        close.setOnClickListener(this);
        set.setOnClickListener(this);
        container.setOnClickListener(this);
    }

    private void close() {
        Intent intent = new Intent(getContext(), MyRewardActivity.class);
        intent.putExtra(gotoUtil.INTENT_VALUE_ONE, 0);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.fast_fade_in, R.anim.fast_fade_out);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_cancle:
                close();
                break;
            case R.id.tv_finish:
                if (!click_flag) {
                    setintro();
                }
                break;
            case R.id.container:
                close();
                break;
        }
    }


    private void setintro() {
        click_flag = true;
        String url = HttpUtils.BASE_URL + GiftApi.getSetPayInfo();
        List<BcyPair> pairs = new ArrayList<>();
        pairs.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(this).getToken()));
        pairs.add(new BcyPair(HttpUtils.GROUP_INTRO, content_et.getText().toString().trim()));
        Map<String, String> map = HttpUtils.getData(pairs);
        Response.Listener<String> onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                click_flag = false;
                if (StatusCodeUtils.dealStatusCode(response, getContext())) {
                    close();
                }
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                click_flag = false;
            }
        };
        StringRequestParam requestParam = new StringRequestParam(Request.Method.POST, url, map, onSucc, onError);
        queue.add(requestParam);
    }

    @Override
    public void onBackPressed() {
        close();
    }
}
